package logic

import (
	"context"
	"tieba/models/mysql"

	"tieba/tiebarpc/internal/svc"
	"tieba/tiebarpc/tiebarpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type TieZiListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewTieZiListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TieZiListLogic {
	return &TieZiListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *TieZiListLogic) TieZiList(in *tiebarpc.TieZiListRequest) (*tiebarpc.TieZiListResponse, error) {
	// todo: add your logic here and delete this line
	offset := (in.Page - 1) * in.Size
	var tz []mysql.TieZiData
	mysql.DB.Debug().Model(&mysql.TieZi{}).Offset(int(offset)).Limit(int(in.Size)).
		Select("tie_zis.*, tie_bas.name,users.user_name").
		Joins("left join tie_bas on tie_bas.id = tie_zis.tie_id").
		Joins("left join users on users.id = tie_zis.user_id").
		Find(&tz)
	var b []*tiebarpc.TzList
	for _, c := range tz {
		list := tiebarpc.TzList{
			TieZiId:  int64(c.ID),
			Name:     c.Name,
			UserName: c.UserName,
			Title:    c.Title,
			Content:  c.Content,
		}
		b = append(b, &list)
	}

	return &tiebarpc.TieZiListResponse{
		List: b,
	}, nil
}
